package com.stox.core.ui.swing.action;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import com.stox.core.manager.ExchangeManager;
import com.stox.core.model.Exchange;
import com.stox.core.ui.swing.wrapper.JTable;
import com.stox.ioc.Container;
import com.stox.resources.i18n.MessageSource;

public class UpdateExchangeSelectionAction implements ActionListener {

	private final JTable table;
	private ExchangeManager exchangeManager;
	
	public UpdateExchangeSelectionAction(JTable table) {
		this.table = table;
		exchangeManager = Container.get(ExchangeManager.class);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		int[] selectedRows = table.getSelectedRows();
		if(selectedRows != null && selectedRows.length > 0){
			List<Exchange> exchanges = new ArrayList<Exchange>(selectedRows.length);
			for(int selectedRowIndex : selectedRows){
				String name = (String)table.getValueAt(selectedRowIndex, 0);
				exchanges.add(exchangeManager.findByName(name));
			}
			exchangeManager.configureExchanges(exchanges);
		}else{
			JOptionPane.showInputDialog(MessageSource.getMessage("Select.at.least.one.exchange"));
		}
	}

}
